package com.bird.generate.ddd.domain.generateutil

import cn.hutool.core.io.FileUtil
import cn.hutool.core.util.StrUtil
import com.bird.generate.ddd.domain.*
import java.time.LocalDate
import java.time.LocalTime
import java.time.format.DateTimeFormatter

class ClientGenerate {

    companion object {
        /**
         * 生成client代码
         */
        fun clientGenerate(path: String,aggregateRoot: AggregateRootEntity) {
            var clientRootPath = path.appendNextPath(aggregateRoot.moduleName.lowercase()+"bc").appendNextPath("${aggregateRoot.moduleName.lowercase()}bc-client").appendNextPath(JAVA_PATH)
                .appendNextPath("${aggregateRoot.comPrefix.replace(".","/")}/${aggregateRoot.projectName.lowercase()}prd/${aggregateRoot.moduleName.lowercase()}bc/client/${aggregateRoot.enName.lowercase()}biz")
            //root 包路径
            var clientJavaPath =
                "${aggregateRoot.comPrefix.replace(".","/")}/${aggregateRoot.projectName.lowercase()}prd/${aggregateRoot.moduleName.lowercase()}bc/client/${aggregateRoot.enName.lowercase()}biz".replace(
                    "/",
                    "."
                )
            //创建pom.xml
            generateClientPomXml(aggregateRoot,path.appendNextPath(aggregateRoot.moduleName.lowercase()+"bc").appendNextPath("${aggregateRoot.moduleName.lowercase()}bc-client"))
            //创建client
            generateClient(aggregateRoot, clientRootPath, clientJavaPath)
            //创建包路径
            generatePackageInfo(clientRootPath.substringBeforeLast("/"), clientJavaPath.substringBeforeLast("."))
            //创建pl文件夹
            generatePlFolder(clientRootPath)

        }

        private fun generateClient(aggregateRoot: AggregateRootEntity, clientPath: String, javaPath:String) {

            val clientContent = """
package ${javaPath};


import com.zz.core.tool.api.R;

 /**
  * ${aggregateRoot.name} 客户端
  * @author ${aggregateRoot.creator}<p>
  * ================================<p>
  * Date: ${LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"))}<p>
  * Time: ${LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm"))}<p>
  * ================================
  */
public interface ${StrUtil.upperFirst(aggregateRoot.enName)}Client {
    
}
                """
            createFile(clientPath.appendNextPath("${StrUtil.upperFirst(aggregateRoot.enName)}Client.java"), clientContent)

        }

        private fun generatePlFolder(path: String) {
            FileUtil.mkdir(path.appendNextPath("pl/"))
        }


        private fun generateClientPomXml(
            aggregateRoot: AggregateRootEntity,
            path: String
        ) {
            val xmlContent =
"""<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>${aggregateRoot.comPrefix}</groupId>
        <artifactId>${aggregateRoot.moduleName.lowercase()}bc</artifactId>
        <version>${aggregateRoot.version}</version>
    </parent>

    <artifactId>${aggregateRoot.moduleName.lowercase()}bc-client</artifactId>

</project>
                """
            createFile(path.appendNextPath("pom.xml"), xmlContent)
        }

    }

}